name: Sync and Merge Files

# 触发条件：每小时的58分钟运行一次，并允许手动触发
on:
  schedule:
    - cron: '56 * * * *' # 每小时的59分钟触发一次
  workflow_dispatch: # 允许手动触发

jobs:
  sync-and-merge:
    runs-on: ubuntu-latest # 使用最新的 Ubuntu 环境

    steps:
    # 步骤1：检出当前仓库代码
    - name: Checkout repository
      uses: actions/checkout@v2

    # 步骤2：配置 Git 用户信息
    - name: Set up Git
      run: |
        git config --global user.name "vbskycn"
        git config --global user.email "88164962@qq.com"

    # 步骤3：下载指定文件
    - name: Download files
      env:
        TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
      run: |
        mkdir -p /tmp/tv # 创建临时目录存放下载的文件
        curl -H "Authorization: token $TOKEN" -o /tmp/tv/jxdx_hd.txt https://raw.githubusercontent.com/vbskycn/iptv/master/dsyy/jxdx_hd.txt
        curl -H "Authorization: token $TOKEN" -o /tmp/tv/iptv6.txt https://raw.githubusercontent.com/vbskycn/iptv6/master/iptv6.txt
        curl -H "Authorization: token $TOKEN" -o /tmp/tv/iptv4.txt https://raw.githubusercontent.com/vbskycn/iptv4/master/iptv4.txt
        curl -H "Authorization: token $TOKEN" -o /tmp/tv/jxdx_hd.m3u https://raw.githubusercontent.com/vbskycn/iptv/master/dsyy/jxdx_hd.m3u
        curl -H "Authorization: token $TOKEN" -o /tmp/tv/iptv4.m3u https://raw.githubusercontent.com/vbskycn/iptv4/master/iptv4.m3u
        curl -H "Authorization: token $TOKEN" -o /tmp/tv/iptv6.m3u https://raw.githubusercontent.com/vbskycn/iptv6/master/iptv6.m3u

    # 步骤4：同步文件到 /tv 目录
    - name: Sync files to /tv
      run: |
        mkdir -p tv # 确保目标目录存在
        cp /tmp/tv/jxdx_hd.txt tv/jxdx_hd.txt
        cp /tmp/tv/iptv4.txt tv/iptv4.txt
        cp /tmp/tv/iptv6.txt tv/iptv6.txt
        cp /tmp/tv/jxdx_hd.m3u tv/jxdx_hd.m3u
        cp /tmp/tv/iptv4.m3u tv/iptv4.m3u
        cp /tmp/tv/iptv6.m3u tv/iptv6.m3u

    # 步骤5：修改并合并 .txt 文件
    - name: Modify and merge .txt files
      run: |
        mkdir -p dsyy # 确保目标目录存在
        # 修改 .txt 文件内容，在 ,#genre# 前添加前缀
        sed 's/,#genre#/jxH&/' /tmp/tv/jxdx_hd.txt > /tmp/tv/jxdx_hd_modified.txt
        sed 's/,#genre#/ip6&/' /tmp/tv/iptv6.txt > /tmp/tv/iptv6_modified.txt
        sed 's/,#genre#/ip4&/' /tmp/tv/iptv4.txt > /tmp/tv/iptv4_modified.txt
        
        # 合并修改后的文件
        cat /tmp/tv/jxdx_hd_modified.txt /tmp/tv/iptv6_modified.txt /tmp/tv/iptv4_modified.txt > /tmp/tv/hd.txt
        if ! cmp -s /tmp/tv/hd.txt dsyy/hd.txt; then # 比较合并后的文件是否不同
          cp /tmp/tv/hd.txt dsyy/hd.txt # 文件不同则覆盖
        fi
        cp /tmp/tv/hd.txt tv/hd.txt # 将合并后的 hd.txt 文件复制到 /tv 目录下

    # 步骤6：提交并推送更改
    - name: Commit and push changes
      run: |
        if [ -n "$(git status --porcelain)" ]; then
          git add tv/jxdx_hd.txt tv/iptv4.txt tv/iptv6.txt tv/jxdx_hd.m3u tv/iptv4.m3u tv/iptv6.m3u tv/hd.txt dsyy/hd.txt # 添加所有更改的文件
          git commit -m "Sync and merge files" # 提交更改
          git push # 推送到远程仓库
        else
          echo "No changes to commit"
        fi
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 使用 GitHub 提供的令牌进行认证
